package com.atmilan.quartz.job;

import com.atmilan.quartz.constant.Constants;
import com.atmilan.quartz.constant.enums.SyncStatusEnum;
import com.atmilan.quartz.model.entity.crmeb.SystemConfig;
import com.atmilan.quartz.service.IPaoloSyncService;
import com.atmilan.quartz.service.ISystemConfigService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * paolo商品同步
 * @Author: wangzh
 * @Date: 2023/2/8 14:32
 */

@Slf4j
@Component
@Configuration
@EnableScheduling
public class PaoloSyncTask {
    @Autowired
    BaseTask baseTask;
    @Autowired
    ISystemConfigService systemConfigService;
    @Autowired
    IPaoloSyncService paoloSyncService;

    @XxlJob("paoloSyncTask")
    public void syncTask(){
        try {
            SystemConfig systemConfig = systemConfigService.getOne(Wrappers.<SystemConfig>lambdaQuery().eq(SystemConfig::getName, Constants.PAOLO_SYNC_STATUS).last("limit 1"));
            SyncStatusEnum paoloSyncStatus = SyncStatusEnum.valueOf(systemConfig.getFormId());
            long timeDiff = new Date().getTime() - systemConfig.getUpdateTime().getTime();

            log.warn("--------------- 开始计划同步PAOLO商品, 同步状态为: {} ---------------", paoloSyncStatus);
            // 24小时内状态还未变更，直接重新处理
            if (SyncStatusEnum.WAIT == paoloSyncStatus && 24 < (timeDiff / 1000 / 60 / 60)) {
                log.warn("--------------- PAOLO商品同步过程超过24小时 ---------------");
                paoloSyncStatus = SyncStatusEnum.PUSH;
            }
            // 有同步任务时不进行处理
            if (SyncStatusEnum.WAIT == paoloSyncStatus) {
                log.warn("--------------- PAOLO任务未结束, 新任务不执行 ---------------");
                return;
            }
            // 更新任务状态为同步中
            baseTask.updateSyscStatus(Constants.PAOLO_SYNC_STATUS, SyncStatusEnum.WAIT, systemConfig);
            // 下发完成，需要同步
            if (SyncStatusEnum.PUSH == paoloSyncStatus) {
                Boolean result = paoloSyncService.syncPaoloProduct();
                if (result) {
                    baseTask.updateSyscStatus(Constants.PAOLO_SYNC_STATUS, SyncStatusEnum.SYNC, systemConfig);
                    log.warn("--------------- PAOLO商品同步完成!!! ---------------");
                }
            // 同步完成, 需要下发
            } else if (SyncStatusEnum.SYNC == paoloSyncStatus) {
                Boolean result = paoloSyncService.pushPaoloProduct();
                if (result) {
                    baseTask.updateSyscStatus(Constants.PAOLO_SYNC_STATUS, SyncStatusEnum.PUSH, systemConfig);
                    log.warn("--------------- PAOLO商品下发完成!!! ---------------");
                }
            }
        } catch (Exception e){
            e.printStackTrace();
        }
    }

}
